package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色服务类
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;    //角色数据类对象

    //查询所有角色信息
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page all = roleDao.findByCondition(queryString);
        long total = all.getTotal();
        List result = all.getResult();
        return new PageResult(total, result);
    }

    //添加角色
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //1.调用roleDao添加角色基本信息
        roleDao.add(role);
        //2.设置角色和权限的关联
        setRoleAndPermission(role.getId(), permissionIds);
        //3.设置角色和菜单的关联
        setRoleAndMenu(role.getId(), menuIds);

    }

    //根据id查询角色
    public Role findById(Integer roleId) {
        return roleDao.findById(roleId);
    }

    //查询角色权限的关联
    public Integer[] findPermissionIdsByRoleId(Integer roleId) {
        return roleDao.findPermissionIdsByRoleId(roleId);
    }

    //查询角色菜单的关联
    public Integer[] findMenuIdsByRoleId(Integer roleId) {
        return roleDao.findMenuIdsByRoleId(roleId);
    }

    //编辑角色
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //1.编辑角色基本信息
        roleDao.edit(role);
        //2.删除角色和权限的关联
        roleDao.deletePermissionIdsByRoleId(role.getId());
        //3.删除角色和菜单的关联
        roleDao.deleteMenuIdsByRoleId(role.getId());
        //4.重新添加角色和权限的关联
        setRoleAndPermission(role.getId(), permissionIds);
        //5.重新添加角色和菜单的关联
        setRoleAndMenu(role.getId(), menuIds);

    }

    //删除角色
    public void deleteById(Integer id) {
        //1.删除角色和权限的关联
        roleDao.deletePermissionIdsByRoleId(id);
        //2.删除角色和菜单的关联
        roleDao.deleteMenuIdsByRoleId(id);
        //3.删除角色基本信息
        roleDao.deleteById(id);

    }

    //设置角色和菜单的关联
    private void setRoleAndMenu(Integer id, Integer[] menuIds) {
        if (menuIds != null & menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map map = new HashMap();
                map.put("roleId", id);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);

            }
        }
    }

    //设置角色和权限的关联
    private void setRoleAndPermission(Integer id, Integer[] permissionIds) {
        if (permissionIds != null & permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap();
                map.put("roleId", id);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);

            }
        }
    }


}
